package com.verifycode.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.verifycode.service.VerifyCodeService;
import com.verifycode.utils.VerifyCode;

@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    private Logger Log = LoggerFactory.getLogger(VerifyCodeServiceImpl.class);

	@Autowired
	VerifyCode vcode;
	
	@Value("${file.path}")  
	private String filePath ;
	
	@Override
	public String createVerifyCode() {
		BufferedImage image = vcode.getImage();
		try {
			String uuid = UUID.randomUUID().toString();
			File dir = new File(filePath);
			Log.info("判断文件目录是否存在:{}",dir.exists());
            if(!dir.exists()){//判断文件目录是否存在  
            	Log.info("创建该目录！");
                dir.mkdirs();  
            }  
			File file = new File(filePath+uuid+".jpg");
			if(!file.exists()) {
				file.createNewFile();
			}
			VerifyCode.output(image, file);
			return vcode.getText();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
